package com.centaline.androidsalesblog.activities.entrust;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.mine.BindingPhoneActivity;
import com.centaline.androidsalesblog.activities.mine.LoginActivity;
import com.centaline.androidsalesblog.adapter.TabFragmentAdapter;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.events.ReservePosition;
import com.centaline.androidsalesblog.fragments.entrust.ReservationEditFragment;
import com.centaline.androidsalesblog.fragments.entrust.ReservationFragment;
import com.centaline.androidsalesblog.model.databaseobject.UserDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centanet.framework.interfaces.ISnack;
import com.trello.rxlifecycle.ActivityEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 约看列表
 */
public class ReservationListActivity extends BaseActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private int currentPosition;//当前选择

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_entrust_order;
    }

    @Override
    protected void findViews() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initViews() {
        setUniversalToolbar();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        currentPosition = getIntent().getIntExtra(ExtraConstant.EXTRA_POSITION, 0);
        checkLogin();

    }

    private void checkLogin() {
        if (AppConstant.IsLogin) {
            RxDataBase.getUser()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .compose(this.<UserDo>bindUntilEvent(ActivityEvent.DESTROY))
                    .subscribe(new Action1<UserDo>() {
                        @Override
                        public void call(UserDo userDo) {
                            if (userDo != null)
                                if (TextUtils.isEmpty(userDo.getPhone())) {
                                    startActivityForResult(new Intent(ReservationListActivity.this, BindingPhoneActivity.class),
                                            CommonConstant.REQ_BIND_MOBILE);
                                } else {
                                    addList();
                                }
                        }
                    });
        } else {
            startActivityForResult(new Intent(ReservationListActivity.this, LoginActivity.class),
                    CommonConstant.REQ_LOGIN);
        }
    }

    private void addList() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> titles = new ArrayList<>();

        ReservationEditFragment reservationEditFragment = new ReservationEditFragment();
        reservationEditFragment.setISnack(new ISnack() {
            @Override
            public void showSnack(CharSequence text) {
                snack(text);
            }
        });
        fragments.add(reservationEditFragment);
        titles.add("未约看");
        ReservationFragment reservationFragment = new ReservationFragment();
        fragments.add(reservationFragment);
        titles.add("已约看");

        viewPager.setAdapter(new TabFragmentAdapter(getSupportFragmentManager(), fragments, titles));
        tabLayout.setupWithViewPager(viewPager);

        Observable.interval(0, 1, TimeUnit.SECONDS).take(1)
                .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        if (currentPosition != 0) {
                            viewPager.setCurrentItem(currentPosition, true);
                        }
                    }
                });
    }

    @Subscribe
    public void onEventMainThread(ReservePosition reservePosition) {
        int position = reservePosition.getPosition();
        viewPager.setCurrentItem(position, true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            switch (requestCode) {
                case CommonConstant.REQ_LOGIN:
                case CommonConstant.REQ_BIND_MOBILE:
                    checkLogin();
                    break;
                default:
                    break;
            }
        } else {
            finish();
        }
    }
}
